package cas.ihep.hdfs.impl;

import cas.ihep.hdfs.HdfsBlock;
import cas.ihep.hdfs.HdfsFile;
import cas.ihep.hdfs.HdfsSystem;
import org.apache.hadoop.hdfs.protocol.LocatedBlock;
import org.apache.hadoop.hdfs.protocol.LocatedBlocks;

import java.io.IOException;

public class HdfsFileIn extends HdfsFile{
    public HdfsFileIn(HdfsSystem fs, String path) throws IOException {
        super(fs, path);
        LocatedBlocks blocks=mFs.getDFSClient().getLocatedBlocks(path,0, mMetadata.getLen());
        //mBlocks=new ArrayList<>(blocks.getLocatedBlocks().size());
        for (LocatedBlock blk:blocks.getLocatedBlocks()) {
            mBlocks.add(mCloser.register(new HdfsReadableBlock(this, blk)));
        }
    }

    @Override
    public HdfsBlock getBlock(int idx) {
        return mBlocks.get(idx);
    }

    @Override
    public long getLength() {
        return mMetadata.getLen();
    }

    @Override
    public HdfsFile open() throws IOException {
        return this;
    }

    @Override
    public boolean canRead() {
        return true;
    }

    @Override
    public boolean carWrite() {
        return false;
    }
}
